// src/utils/User.js
import apiClient from '@/apis/axios.js'
import {AuthApi} from "@/apis/authApi.js";
class User {
    /**
     * 检查用户是否已登录（通过检查 localStorage 中的 token）
     * @returns {boolean} - 如果有 token 则返回 true，否则返回 false
     */
    static isLogin() {
        return !!localStorage.getItem('fan_video_token');
    }

    /**
     * 存储用户的 token 到 localStorage
     * @param {string} token - 用户的 token
     */
    static setToken(token) {
        if (token) {
            localStorage.setItem('fan_video_token', token);
        } else {
            console.warn('设置token不能为空');
        }
    }

    /**
     * 存储用户的 id 到 localStorage
     * @param id
     */
    static setUserId(id) {
        if(id){
            localStorage.setItem('fan_video_userid', id);
        } else {
            console.warn('设置userid不能为空');
        }
    }


    static async login(userAccount, Password, loginType="666", permissionType="666") {
        const params = { userAccount, Password, loginType, permissionType }
        try {
            const res = await AuthApi.login(params);
            this.setUserId(res.data.Userinfo.userid)
            return res.data;
        } catch (error) {
            console.error('Login failed:', error);
            throw error;
        }
    }

    static async getUserInfo(){
        const user_id = localStorage.getItem('fan_video_userid');
        const res = await AuthApi.fetchUserInfo(user_id);
        console.log(res)
        return res.data[0];
    }

    static logout() {
        localStorage.removeItem('fan_video_token');
        localStorage.removeItem('fan_video_userid');
    }

    static uploadAvatar() {}
}

export default User;
